<?php

namespace app\service;

use app\model\SuperChatModel;
use think\facade\Log;

class SuperChatService extends BaseService
{
    protected SuperChatModel $scModel;
    protected function initialize() {
        $this->scModel = new SuperChatModel();
    }

    public function insertSC($info): bool
    {
        echo "========================================",PHP_EOL;
        $data = [];
        $userIdLis = (new UserService())->getUserIdList();
        $num = count($info["scList"]);
        $i = 1;
        foreach ($info["scList"] as $value) {
            echo "醒目留言解析ing " . $i++ . "/{$num} {$value['content']}",PHP_EOL;
            if(!$this->scModel->where([["room_id","=",$info["fileInfo"]["room_id"]],["user_id","=",$userIdLis[$value["uuid"]]],["tid","=",$value["tid"]],["content","=",$value["content"]],["price","=",$value["price"]]])->count()) {
                $data[] = [
                    "room_id" => $info["fileInfo"]["room_id"],
                    "file_date" => $info["fileInfo"]["file_date"],
                    "gift_id"       => $value["gift_id"],
                    "gift_name"     => $value["gift_name"],
                    "user_id"   => $userIdLis[$value["uuid"]],
                    "content" => $value["content"],
                    "tid"       => $value["tid"],
                    "price"     => $value["price"],
                    "user_level"     => $value["user_level"],
                    "create_time" => date("Y-m-d H:i:s"),
                    "update_time" => date("Y-m-d H:i:s")
                ];
            }
        }
        echo "醒目留言解析ing 存入数据库",PHP_EOL;
        try {
            $result = $this->scModel->saveAll($data);
        } catch (\Exception $e) {
            Log::error(date("Y-m-d H:i:s")."存入醒目留言失败 详情:" . $e->getMessage());
            exit();
        }
        return (bool)$result;
    }
}